var revenueApi = require('../../script/revenue.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CONS_NO: '',
    CONS_NAME: '',
    WATER_ADDR: '',
    HeatYearList: [],
    Total: 0,
    noData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      CONS_NO: options.CONS_NO,
      CONS_NAME: options.CONS_NAME,
      WATER_ADDR: options.addr || ''
    });
    this.getHeatYearList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getHeatYearList() {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    revenueApi.HeatYearNoPay(this.data.CONS_NO).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        const resData = res.data && res.data.model || [];
        const list = [];
        resData.forEach(item => {
          if (item._ARREARS_THIS != null && item._ARREARS_THIS > 0) {
            list.push({
              HEAT_DURATION: item.HEAT_DURATION,
              _ARREARS_THIS: item._ARREARS_THIS,
              AREA_NAME: item.AREA_NAME,
              COST_ID: item.COST_ID,
              Selected: false
            })
          }
        });
        let Total = 0;
        if(list.length > 0){
          list[0].Selected = true;
          Total = list[0]._ARREARS_THIS
        }
        this.setData({
          HeatYearList: list,
          noData: list.length === 0,
          Total
        });
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  selectRow(e) {
    var row = e.currentTarget.dataset.row;
    const dataList = [...this.data.HeatYearList];
    const val = dataList.find(item => item.COST_ID === row.COST_ID);
    let Total = this.data.Total;
    if (val.Selected && val.Selected == true) {
      val.Selected = false;
      Total -= val._ARREARS_THIS
    } else {
      val.Selected = true;
      Total += val._ARREARS_THIS
    }
    Total = Math.round(Total * 10) / 10;
    this.setData({
      HeatYearList: dataList,
      Total
    });
  },
  toPay() {
    // wx.navigateTo({
    //   url: "../user-pay/user-pay",
    // })
    // return
    if (this.data.noData) {
      wx.showToast({
        title: '用户不欠费',
        icon: 'none'
      });
      return;
    }
    if (this.data.Total === 0) {
      wx.showToast({
        title: '请选择收费单',
        icon: 'none'
      });
      return
    }
    const ids = [];
    this.data.HeatYearList.forEach(item => {
      if (item.Selected) {
        ids.push(item.COST_ID)
      }
    });
    const params = {
      ConsNo: this.data.CONS_NO,
      ConsName: this.data.CONS_NAME,
      CostIdList: ids
    }
    revenueApi.CreateQROrder(params).then(res => {
      if (res.code === 200) {
        wx.navigateTo({
          url: "../user-pay/user-pay",
          success: (res1) => {
            // 通过eventChannel向被打开页面传送数据
            const sendData = res.data;
            sendData.CONS_NO = params.ConsNo;
            sendData.CONS_NAME = params.ConsName;
            res1.eventChannel.emit('sendmessage', {
              data: sendData
            })
          }
        })
      } else {
        wx.showToast({
          title: res.info,
          icon: 'none'
        });
      }
    })
  },
})